package com.zhiyume.controller;

import com.zhiyume.dao.entity.TAdminSys;
import com.zhiyume.dto.AuthritoryDTO;
import com.zhiyume.dto.UserDTO;
import com.zhiyume.exception.IncorrectPasswordException;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.EncryptUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import com.zhiyume.shiro.ShiroToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jiaxz
 * @javaDoc 登陆controller
 */
@RestController
public class AuthorityController {
    private static final Logger logger = LoggerFactory.getLogger(AuthorityController.class);

    private static final String MODULE = AuthorityController.class.getName();

    @RequestMapping("/ajaxLogin")
    public String login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        if (StringUtil.isBlank(userName) || StringUtil.isBlank(password)) {
            return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_MISS_PARA.getErrType(), ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        try {
            ShiroToken shiroToken = new ShiroToken();
            shiroToken.setUsername(userName);
            shiroToken.setPassword(EncryptUtil.SHA256(password).toCharArray());
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            session.setTimeout(10800000);
            subject.login(shiroToken);
            logger.info( subject.getPrincipal()+"");
            logger.info( subject.getSession()+"");
            logger.info( subject.isAuthenticated()+"");

        } catch (IncorrectPasswordException exception) {
            return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_ERROR_PASSWORD.getErrType(), ConstantEnum.CONSTANT_ERROR_PASSWORD.getErrMsg());
        }
        //subject.logout();
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
    }


    @RequestMapping("/getAuthrotity")
    public AuthritoryDTO authrotity() {
        TAdminSys sysUser = AdminSecurityManager.getSessionTAdminSys();
        /**获取权限*/
        List menuList = sysUser.getMenuList();
        if (menuList == null || menuList.size() == 0) {
            AdminSecurityManager.isSessionUserAdminRole();
            menuList = sysUser.getMenuList();
        }
        AuthritoryDTO authritoryDTO = new AuthritoryDTO();
        if (menuList == null || menuList.size() == 0) {
            //TODO 返回用户没有权限
            authritoryDTO.setResult("fail");
            authritoryDTO.setResultMsg("用户没有权限");
            return authritoryDTO;
        }
       /* List muList = new ArrayList();
        for(TMenu tmenu : menuList) {
            MenuDTO menuDTO = new MenuDTO();
            menuDTO.setHref(tmenu.getUrl());
            menuDTO.setIcon(tmenu.getIcon());
            menuDTO.setTitle(tmenu.getMenuName());
            menuDTO.setSpread(String.valueOf(Boolean.FALSE));
            muList.add(menuDTO);
        }
*/
        //List<CustPhone> list = new ArrayList<CustPhone>();
        //String str= JSON.toJSON(menuList).toString();
        //JSONArray jsonArray = new JSONArray(menuList);
        UserDTO userDTO = new UserDTO();
        userDTO.setId(sysUser.getId());
        userDTO.setUserName(sysUser.getUserName());
        userDTO.setName(sysUser.getNickName());
        authritoryDTO.setUser(userDTO);
        authritoryDTO.setMenu(menuList);
        authritoryDTO.setResult("success");
        authritoryDTO.setResultMsg("成功");
        return authritoryDTO;
    }
}
